<template>
	<view :class="['auto-wrapper',show?'auto-coupon-modal':'']">
		<template v-if="receivedList.length > 0">
			<view class="coupon-list">
				<view class="coupon-box" v-for="(item,index) in receivedList" :key="index">
					<view class="coupon-left">
						<view class="coupon-leftWord">
							￥{{item.couponPrice}}
						</view>
						<view class="coupon-left-small" v-if="item.couponType == 1">满减券</view>
						<view class="coupon-left-small" v-if="item.couponType == 2">代金券</view>
					</view>
					<view class="coupon-right">
						<view class="coupon-name" v-if="item.couponLimit">
							满{{item.couponLimit}}元可用
						</view>
						<view class="coupon-date">
							有效期：{{item.couponStart}}至{{item.couponEnd}}
						</view>
					</view>
				</view>
			</view>
			<i class="iconfont iconguanbi1" style="font-size:30px;color:#fff;" @tap="closeModel"></i>
		</template>
		<view class="all-error" v-if="allErrorModal">
			<view class="tui-modal-title">提示</view>
			<view class="tui-modal-content">{{modalContent}}</view>
			<view class="tui-modalBtn-box" style="justify-content: center;">
				<button class="tui-modal-btn" hover-class="tui-primary-hover" @tap="hideAllErrorModal">确定</button>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		props: {
			idList: {
				type: Array
			},
			show: {
				type: Boolean
			},
			flag:{
				type: String
			}
		},
		data() {
			return {
				buttonList: [{
					text: "确定",
					type: "primary",
					plain: false
				}],
				receivedList: [],
				allErrorModal: false,
				modalContent: '',
			};
		},
		watch: {
			idList(val) {
				console.log(val)
				if (val.length > 0&&this.flag!=2) {
					let list = [];
					val.forEach(i => {
						const api = this.$http.receiveCoupon({
							couponId: i,
							receiveMode: '1'
						});
						list.push(api);
					});
					Promise.all(list).then((res) => {
						console.log(res);
						if (res.every(i => i.status == 1 && i.code == 'Msg.DRMALL.ERR.012')) {
							// 全部领取失败
							this.receivedList = [];
							this.allErrorModal = true;
							this.modalContent = res[res.length - 1].message;
						}else if (res.every(i => i.status == 1 && i.code != 'Msg.DRMALL.ERR.012')) {
							// 除领取失败外错误
							this.receivedList = [];
							this.closeModel();
						} else {
							this.receivedList = [];
							res.forEach(i => {
								if (i.status == 0) {
									this.receivedList.push(i.data);
								}
							})
						}
					});
				}else if(val.length > 0&&this.flag==2){
					this.receivedList = [];
					val.forEach(i => {
							this.receivedList.push(i);
					})
				}
			}
		},
		methods: {
			hideAllErrorModal() {
				this.allErrorModal = false;
				this.closeModel();
			},
			closeModel() {
				this.receivedList = [];
				this.$emit('update:show', false);
				this.$emit('update:idList', []);
			}
		}
	}
</script>

<style lang="scss">
	.auto-wrapper {
		.all-error {
			background: #fff;
			z-index: 9999996;
			border-radius: 24rpx;
			padding: 40rpx 64rpx;
			width: 84%;
		
			.tui-modal-title {
				text-align: center;
				font-size: 34rpx;
				color: #333;
				padding-top: 20rpx;
				font-weight: bold;
			}
		
			.tui-modal-content {
				text-align: center;
				color: #999;
				font-size: 28rpx;
				padding-top: 20rpx;
				padding-bottom: 60rpx;
			}
		
			.tui-modalBtn-box {
				width: 100%;
				display: flex;
				align-items: center;
				justify-content: space-between
			}
		
			.tui-modal-btn {
				width: 46%;
				height: 68rpx;
				line-height: 68rpx;
				position: relative;
				border-radius: 10rpx;
				font-size: 24rpx;
				overflow: visible;
				margin-left: 0;
				margin-right: 0;
				background: linear-gradient(151deg, #F69E97 0%, #EA655E 100%) !important;
				color: #fff;
			}
		
			.tui-primary-hover {
				background: #4a67d6;
				color: #e5e5e5;
			}
		}
	}
	.auto-coupon-modal {
		z-index: 9999996;
		position: fixed;
		top: 0;
		left: 0;
		right: 0;
		bottom: 0;
		background: rgba(0, 0, 0, 0.6);
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: center;
		
		.coupon-list {
			margin-bottom: 20rpx;
			width: 80%;
			padding: 10rpx;
			max-height: 66vh;
			overflow-y: auto;
		}

		.coupon-box {
			background: #FFFFFF;
			border-radius: 20rpx;
			border: 1px solid #B2B2B2;
			display: flex;
			margin-bottom: 10rpx;

			.coupon-left {
				width: 168rpx;
				border-right: 1px dashed #B2B2B2;
				text-align: center;
				display: flex;
				flex-direction: column;
				justify-content: center;

				.coupon-leftWord {
					color: #EA655E;
					font-size: 40rpx;
				}

				.coupon-left-small {
					color: #999999;
					font-size: 24rpx;
				}
			}

			.coupon-right {
				flex: 1;
				padding: 20rpx;

				.coupon-name {
					color: #EA655E;
					font-size: 30rpx;
					width: 300rpx;
					overflow: hidden;
					text-overflow: ellipsis;
					white-space: nowrap
				}

				.coupon-date {
					color: #999999;
					font-size: 24rpx;
				}
			}
		}

		.iconfont {
			color: #fbd977;
		}
	}
</style>
